package com.luzhikun.kickUser;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符集 在post请求中用setCharacterEncoding就可以
        request.setCharacterEncoding("UTF-8");
        // 获取参数
        String name = request.getParameter("username");
        String agestr = request.getParameter("age");
        Integer age = Integer.parseInt(agestr);
        // 创建User对象
        User user = new User(name, age);

        //获取当前请求的session
        HttpSession session = request.getSession();

        // 将user与session绑定
        session.setAttribute("user", user);

        // 跳转到欢迎页面
        response.sendRedirect(request.getContextPath() + "/welcome.jsp");

    }
}
